home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / rc-1.000 / rc-1 / rc-1.5-linux / b_ssub.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-22  |  2.3 KB  |  70 lines

  1. /*  PROGRAM TO SUBSTITUTE SUFFIXES/PREFIXES
  2. ssub [-pb] name s1 [s2]      replaces suffix s1 by suffix s2
  3.      -S strip any leading path in output (i.e. basename behavior)
  4.      -p act on prefix rather than suffix
  5.      if s2 is unspecified, the suffix/prefix is simply deleted
  6.      if no match is found, simply echo the input */
  7.  
  8. #include "addon.h"
  9. typedef enum bool { FALSE, TRUE } bool;
  10. extern void set(bool);
  11.  
  12. #include <string.h>
  13. #include <stdio.h>
  14. void b_ssub(char* av[]) {
  15.     char *name, *s1, *s2, *out, *tmp, ctmp;
  16.     int ac=0, beg=0, strip=0, subst, both, l1, lname, flags;
  17.  
  18.     while (av[ac]!=0) ac++;
  19.  
  20.     if (ac<3 || ac>5) {
  21.         printf("usage: ssub [-pS] name s1 s2\n");
  22.         set(FALSE);
  23.         return;
  24.     }
  25.     if (ac==4 || ac==5) {                               /* parse command line */
  26.         both =  strcmp(av[1],"-Sp")==0 || strcmp(av[1],"-pS")==0;
  27.         beg =   both || strcmp(av[1],"-p")==0;
  28.         strip = both || strcmp(av[1],"-S")==0;
  29.         if (beg||strip) subst=(ac==5); else subst=(ac==4);
  30.     }
  31.  
  32.     flags= (beg||strip) ? 1 : 0;
  33.     name=av[1+flags];
  34.     s1 = av[2+flags];
  35.     s2 = av[3+flags];
  36.     l1 = strlen(s1);
  37.     if (strip)                               /* strip leading path if desired */
  38.         if ((tmp = rindex(name,'/')) != NULL)
  39.             name = tmp + 1;
  40.     lname = strlen(name);
  41.  
  42.     if (beg) {                                  /* remove s1 if it's a prefix */
  43.         ctmp = name[l1];
  44.         name[l1] = '\0';
  45.         if (strcmp(s1,name)==0)                    /* it's a prefix all right */
  46.             out = name+l1;
  47.         name[l1] = ctmp;
  48.     } else {                                    /* remove s1 if it's a suffix */
  49.         if (strcmp(s1,name+lname-l1)==0) {
  50.             out = name;
  51.             out[lname-l1] = '\0';
  52.         }
  53.     }
  54.     if (out == NULL)                                        /* print original */
  55.         printf("%s\n",name);
  56.     else {                                               /* print replacement */
  57.         if (subst&&beg) {
  58.             if (s2==NULL) printf("%s",out); else printf("%s%s",s2,out);
  59.             set(TRUE);
  60.             return;
  61.         }
  62.         if (subst)
  63.             if (s2==NULL) printf("%s",out); else printf("%s%s",out,s2);
  64.         else
  65.             printf("%s",out);
  66.     }
  67.     set(TRUE);
  68.     return;
  69. }
  70.